import { Component, OnInit } from "@angular/core";
import { DatatableComponent } from "@swimlane/ngx-datatable/release";
import { JsonApiService } from "app/core/api/json-api.service";
import { Router, ActivatedRoute } from "@angular/router";

declare var $: any;
@Component({
	selector: "sa-user",
	templateUrl: "./express-company.component.html",
	styleUrls: ["./express-company.css"],
})
export class ExpressCompanyComponent implements OnInit {
	tableData: any = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //分页每页条数
	searchKey: string = ""; //搜索keyword

	formType: string = ""; // 表单类型

	// 提交的表单
	companyForm: any = {
		companyName: "",
		companyCode: "",
		isUsed: 1,
	};

	validatorOptions = {
		excluded: [":disabled"],
		feedbackIcons: {
			valid: "glyphicon glyphicon-ok",
			invalid: "glyphicon glyphicon-remove",
			validating: "glyphicon glyphicon-refresh",
		},
		fields: {
			companyName: {
				trigger:"change",
				validators: {
					notEmpty: {
						message: "请输入公司名称",
					},
				},
			},
			// companyCode: {
			// 	trigger:"change",
			// 	validators: {
			// 		notEmpty: {
			// 			message: "请输入公司编码",
			// 		},
			// 	},
			// },
		},
	};

	constructor(
		private jsonApiService: JsonApiService,
		private router: Router,
		private activatedRoute: ActivatedRoute
	) {
		activatedRoute.queryParams.subscribe((queryParams) => {});
	}
	//初始化钩子函数
	ngOnInit() {
		this.load();
	}
	//刷新
	refresh() {
		this.load();
	}

	//表格搜索
	table_search() {
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				searchKey: this.searchKey,
			},
		};
		let reqdata = "gridPager=" + JSON.stringify(data);
		this.jsonApiService
			.http("orderLogistics/logisticsCompanyList.html", reqdata)
			.subscribe((data) => {
				if (data.isSuccess) {
					this.tableData = data.exhibitDatas;
					this.totalCount = data.recordCount;
				}
			});
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}

	/**
	 * 新增和编辑快递公司
	 */
	newCompany(modal) {
		this.formType = "new";
		$("#companyForm").data("bootstrapValidator").resetForm();
		// this.companyForm = {
		// 	companyName: "",
		// 	companyCode: "",
		// 	isUsed: 1,
		// };
		modal.show();
	}
	//  编辑的弹框打开
	editCompany(row, modal) {
		this.formType = "update";
		$("#companyForm").data("bootstrapValidator").resetForm();
		this.companyForm = JSON.parse(JSON.stringify(row));
		modal.show();
	}
	isUsedChange(status) {
		this.companyForm.isUsed = status;
	}
	// 关闭
	companyClose(modal) {
		$("#companyForm").data("bootstrapValidator").resetForm();
		modal.hide();
		this.companyForm = {
			companyName: "",
			companyCode: "",
			isUsed: 1,
		};
	}
	// 删除快递公司
	deleteCompany(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata= {id:row.id}
			this.jsonApiService.http_obj('orderLogistics/deleteLogisticsCompany.html', reqdata).subscribe((data) => {
				this.jsonApiService.message(data.message);
				// 如果添加成功
				if (data.success) {
					this.load();
				}
			});
		})
	}
	// 提交
	companySubmit(modal) {
		$("#companyForm").bootstrapValidator("validate"); //开启验证
		let isValid = $("#companyForm").data("bootstrapValidator").isValid(); //验证是否通过
		console.log(this.companyForm);
		let url = "";
		if (this.formType == "new") {
			url = "orderLogistics/addLogisticsCompany.html";
			
		} else if (this.formType == "update") {
			url = "orderLogistics/editLogisticsCompany.html";
			
		}
		if (isValid) {
			this.jsonApiService.http_obj(url, this.companyForm).subscribe((data) => {
				this.jsonApiService.message(data.message);
				// 如果添加成功
				if (data.success) {
					this.load();
					this.companyClose(modal);
				}
			});
		}
	}
}
